package com.hl.testdb;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.hl.testdb.db.DBHelper;
import com.hl.testdb.db.bean.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;
    private Executor executor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        executor = Executors.newCachedThreadPool();
    }

    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.insert:
                new Thread(){
                    @Override
                    public void run() {
                        Log.d("Tag", "insert data start.");
                        DBHelper dbhelper = new DBHelper(MyApplication.getInstance());
                        db = dbhelper.getReadableDatabase();
//                        db.enableWriteAheadLogging();
                        db.beginTransaction();
                        ContentValues values;
                        for (int i = 0 ;i < 100000;i++)
                        {
                            values = new ContentValues();
                            values.put("name","hl");
                            values.put("age","26");
                           db.insert("person",null,values);
                        }
                        db.setTransactionSuccessful();

                        db.endTransaction();

                        Log.d("Tag", "insert data end.");
                    }
                }.start();


                break;
            case R.id.query:
                Thread[] threads = new Thread[1];

                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        Cursor cursor = db.query("person",null,null,null,null,null,null);
                        if (cursor == null) return;
                        List<Person> persons = new ArrayList<>();
                        Person person;
                        while (cursor.moveToNext())
                        {
                            person = new Person();
                            person.setService_id(cursor.getInt(cursor.getColumnIndex("service_id")));
                            person.setName(cursor.getString(cursor.getColumnIndex("name")));
                            person.setAge(cursor.getInt(cursor.getColumnIndex("age")));
                            persons.add(person);
                        }
                        cursor.close();
                        Log.d("Tag", "run: query end");
                    }
                });

//                for (int i = 0;i< 1;i++)
//                {
//                    threads[i] = new Thread(){
//                        @Override
//                        public void run() {
////                            List<Person> persons = DBUtils.getAllPersons();
////                            for (Person person:persons)
////                            {
//////                                Log.d("Tag", Thread.currentThread().getName()+"id: "+person.getService_id() +"name: "+person.getName()+" age: "+person.getAge());
////                            }
//
//
//                            Cursor cursor = db.query("person",null,null,null,null,null,null);
//                            if (cursor == null) return;
//                            List<Person> persons = new ArrayList<>();
//                            Person person;
//                            while (cursor.moveToNext())
//                            {
//                                person = new Person();
//                                person.setService_id(cursor.getInt(cursor.getColumnIndex("service_id")));
//                                person.setName(cursor.getString(cursor.getColumnIndex("name")));
//                                person.setAge(cursor.getInt(cursor.getColumnIndex("age")));
//                                persons.add(person);
//                            }
//                            cursor.close();
//                            Log.d("Tag", "run: query end");
//
//                        }
//                    };
//                }

//                for (Thread thread: threads)
//                {
//                    thread.start();
//                }
                break;

        }
    }
}
